package com.example.day03_mvp.fragment;


import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.example.day03_mvp.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class WebFragment extends Fragment {


    private Button mBuPj;
    private ImageView mA;
    private Button mBu1;
    private ImageView mB;

    public WebFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_web, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        mBuPj = view.findViewById(R.id.bu_pj);
        mA = view.findViewById(R.id.a);
        mBuPj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.set);
                mA.startAnimation(animation);
            }
        });
        mBu1 = view.findViewById(R.id.bu1);
        mB = view.findViewById(R.id.b);
        AnimationDrawable animationDrawable1 = new AnimationDrawable();
        //准备好资源图片
        int[] ids = {R.drawable.a0,R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7};
        mBu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //通过for循环添加每一帧动画
                for(int i = 0 ; i < 4 ; i ++){
                    Drawable frame = getResources().getDrawable(ids[i]);
                    //设定时长
                    animationDrawable1.addFrame(frame,200);
                }
                animationDrawable1.setOneShot(false);
                //将动画设置到背景上
                mB.setBackground(animationDrawable1);
                //开启帧动画
                animationDrawable1.start();
            }
        });
    }
}
